<?php

class NVL_Functions
{

    public function ChangeToSlug ($str)
    {
        $unicode = array(
                
                'a' => 'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ',
                
                'd' => 'đ',
                
                'e' => 'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ',
                
                'i' => 'í|ì|ỉ|ĩ|ị',
                
                'o' => 'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ',
                
                'u' => 'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự',
                
                'y' => 'ý|ỳ|ỷ|ỹ|ỵ',
                
                'A' => 'Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
                
                'D' => 'Đ',
                
                'E' => 'É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
                
                'I' => 'Í|Ì|Ỉ|Ĩ|Ị',
                
                'O' => 'Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
                
                'U' => 'Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
                
                'Y' => 'Ý|Ỳ|Ỷ|Ỹ|Ỵ'
        )
        ;
        
        foreach ($unicode as $nonUnicode => $uni) {
            
            $str = preg_replace("/($uni)/i", $nonUnicode, $str);
        }
        $str = preg_replace('([^a-zA-Z0-9\s])', '', $str);
        $str = preg_replace("/\s\s+/", ' ', strtolower(trim($str)));
        $str = preg_replace("/\s/", '-', strtolower(trim($str)));
        return $str;
    }
    public function ChangeToNoSpace ($str)
    {
        $unicode = array(
    
                'a' => 'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ',
    
                'd' => 'đ',
    
                'e' => 'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ',
    
                'i' => 'í|ì|ỉ|ĩ|ị',
    
                'o' => 'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ',
    
                'u' => 'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự',
    
                'y' => 'ý|ỳ|ỷ|ỹ|ỵ',
    
                'A' => 'Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
    
                'D' => 'Đ',
    
                'E' => 'É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
    
                'I' => 'Í|Ì|Ỉ|Ĩ|Ị',
    
                'O' => 'Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
    
                'U' => 'Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
    
                'Y' => 'Ý|Ỳ|Ỷ|Ỹ|Ỵ'
        )
        ;
    
        foreach ($unicode as $nonUnicode => $uni) {
    
            $str = preg_replace("/($uni)/i", $nonUnicode, $str);
        }
        $str = preg_replace('([^a-zA-Z0-9\s])', '', $str);
        $str = preg_replace("/\s\s+/", ' ', (trim($str)));
        $str = preg_replace("/\s/", '', (trim($str)));
        return $str;
    }
    public function vn_str_filter_space ($str)
    {
        $unicode = array(
                
                'a' => 'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ',
                
                'd' => 'đ',
                
                'e' => 'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ',
                
                'i' => 'í|ì|ỉ|ĩ|ị',
                
                'o' => 'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ',
                
                'u' => 'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự',
                
                'y' => 'ý|ỳ|ỷ|ỹ|ỵ',
                
                'A' => 'Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
                
                'D' => 'Đ',
                
                'E' => 'É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
                
                'I' => 'Í|Ì|Ỉ|Ĩ|Ị',
                
                'O' => 'Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
                
                'U' => 'Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
                
                'Y' => 'Ý|Ỳ|Ỷ|Ỹ|Ỵ'
        )
        ;
        
        foreach ($unicode as $nonUnicode => $uni) {
            
            $str = preg_replace("/($uni)/i", $nonUnicode, $str);
        }
        $str = preg_replace('([^a-zA-Z0-9])', '', strtolower(trim($str)));
        $str = preg_replace("/\s\s+/", ' ', strtolower(trim($str)));
        $str = preg_replace("/\s/", '-', strtolower(trim($str)));
        return $str;
    }
    // loai bo khoan trang du thua
    public function MyTrim ($str)
    {
        $str = preg_replace("/\s\s+/", ' ', trim($str));
        return $str;
    }
    // doi '-' => ' '
    public function my_replace ($str)
    {
        $str = preg_replace("/[-]/", ' ', trim($str));
        return $str;
    }

    public function MyTitleTooltip ($str)
    {
        $str = preg_replace("/[']/", '"', trim($str));
        return $str;
    }

    public function get_Datetime_Now ()
    {
        $tz_object = new DateTimeZone('Asia/Ho_Chi_Minh');
        $datetime = new DateTime();
        $datetime->setTimezone($tz_object);
        return $datetime->format('Y-m-d H:i:s');
    }

    public function getNumWord ($str, $num_words)
    {
        $words = array();
        $words = explode(" ", $str, $num_words + 1);
        if (count($words) == $num_words + 1) {
            $words[$num_words] = " ... ";
        }
        return implode(" ", $words);
    }
    // get img
    public function getImange ($content)
    {
        $array = array();
        preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $content, $array);
        if (empty($array)) {
            return PUBLIC_URL . '/img/logogameStudio.jpg';
        } else {
            return $array[1];
        }
    }

    function time_stamp ($datetime)
    {
        $time_ago = strtotime($datetime);
        $cur_time = time();
        $time_elapsed = $cur_time - $time_ago;
        $seconds = $time_elapsed;
        $minutes = round($time_elapsed / 60);
        $hours = round($time_elapsed / 3600);
        $days = round($time_elapsed / 86400);
        $weeks = round($time_elapsed / 604800);
        $months = round($time_elapsed / 2600640);
        $years = round($time_elapsed / 31207680);
        // Seconds
        if ($seconds <= 60) {
            return " Cách đây " . $seconds . " giây ";
        }         // Minutes
        else 
            if ($minutes <= 60) {
                if ($minutes == 1) {
                    return " Cách đây 1 phút ";
                } else {
                    return " Cách đây $minutes phút";
                }
            }             // Hours
            else 
                if ($hours <= 24) {
                    if ($hours == 1) {
                        return "Cách đây 1 tiếng ";
                    } else {
                        return " Cách đây  $hours tiếng ";
                    }
                }                 // Days
                else 
                    if ($days <= 7) {
                        if ($days == 1) {
                            return " Ngày hôm qua ";
                        } else {
                            return " Cách đây  $days ngày ";
                        }
                    }                     // Weeks
                    else 
                        if ($weeks <= 4.3) {
                            if ($weeks == 1) {
                                return " Cách đây 1 tuần ";
                            } else {
                                return " Cách đây  $weeks tuần";
                            }
                        }                         // Months
                        else 
                            if ($months <= 12) {
                                if ($months == 1) {
                                    return " Cách đây 1 tháng ";
                                } else {
                                    return " Cách đây $months tháng";
                                }
                            }                             // Years
                            else {
                                if ($years == 1) {
                                    return " Cách đây 1 năm ";
                                } else {
                                    return " Cách đây $years năm ";
                                }
                            }
    }

    function time_stamp_toDays ($datetime)
    {
        $time_ago = strtotime($datetime);
        $cur_time = time();
        $time_elapsed = $cur_time - $time_ago;
        $days = round($time_elapsed / 86400);
        return $days;
    }

    public function url (array $urlOptions = array(), $name = null, $reset = false, 
            $encode = true)
    {
        $router = Zend_Controller_Front::getInstance()->getRouter();
        return $router->assemble($urlOptions, $name, $reset, $encode);
    }

    public function generateRandomString ($length = 10)
    {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyz!@%^&*<>?:';
        $randomString = '';
        for ($i = 0; $i < $length; $i ++) {
            $randomString .= $characters[rand(0, strlen($characters) - 1)];
        }
        return $randomString;
    }
    
    public function RandomString ($length = 10)
    {
        $characters = '0123!@*456789abcdefghi!@*jklmnopqrstuvwxyz!@*';
        $randomString = '';
        for ($i = 0; $i < $length; $i ++) {
            $randomString .= $characters[rand(0, strlen($characters) - 1)];
        }
        return $randomString;
    }
    // Function to get the client IP address
    function get_client_ip ()
    {
        $ipaddress = '';
        if (getenv('HTTP_CLIENT_IP'))
            $ipaddress = getenv('HTTP_CLIENT_IP');
        else 
            if (getenv('HTTP_X_FORWARDED_FOR'))
                $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
            else 
                if (getenv('HTTP_X_FORWARDED'))
                    $ipaddress = getenv('HTTP_X_FORWARDED');
                else 
                    if (getenv('HTTP_FORWARDED_FOR'))
                        $ipaddress = getenv('HTTP_FORWARDED_FOR');
                    else 
                        if (getenv('HTTP_FORWARDED'))
                            $ipaddress = getenv('HTTP_FORWARDED');
                        else 
                            if (getenv('REMOTE_ADDR'))
                                $ipaddress = getenv('REMOTE_ADDR');
                            else
                                $ipaddress = 'UNKNOWN';
        return $ipaddress;
    }

    function is_image ($path)
    {
        $a = getimagesize($path);
        $image_type = $a[2];
        
        if (in_array($image_type, 
                array(
                        IMAGETYPE_GIF,
                        IMAGETYPE_JPEG,
                        IMAGETYPE_PNG,
                        IMAGETYPE_BMP
                ))) {
            return true;
        }
        return false;
    }

    public function paginator ($data, $itemPage, $pageRange, $currentPage)
    {
        $adapter = new Zend_Paginator_Adapter_DbSelect($data);
        $paginator = new Zend_Paginator($adapter);
        $paginator->setItemCountPerPage($itemPage);
        $paginator->setPageRange($pageRange);
        //$currentPage = $currentPage;
        $paginator->setCurrentPageNumber($currentPage);
        return $paginator;
    }

    public function send_mail ($sendTo, $subject, $body)
    {
        // echo $sendTo." - ".$subject; die;
        $connmail = new Zend_Mail_Transport_Smtp('smtp.gmail.com', 
                array(
                        'auth' => 'login',
                        'username' => 'contact.gamestudio.vn@gmail.com',
                        'password' => 'ifunsoftdt!321',
                        'ssl' => 'ssl',
                        'port' => 465
                ));
        Zend_Mail::setDefaultTransport($connmail);
        $mail = new Zend_Mail('UTF-8');
        $mail->setBodyHtml($body);
        // $mail->addBcc(array('linhvuit@gmail.com'));
        $mail->addTo($sendTo);
        $mail->setSubject($subject);
        $mail->setFrom('KidPlay');
        $mail->send();
    }
    
    public function ifs_encode($str){
        date_default_timezone_set('Asia/Ho_Chi_Minh');
        $code = 'ifsdt99!*101#';
        $re = base64_encode(time().md5($code));
        
        $str = preg_replace("/[.]/", md5('ifsdt99!'), trim($str));
        $re =  $re.base64_encode($str);
        
        $re = $re.base64_encode(md5($code));
        $re = $this->remove_equal($re);
        return $re;
    }
    public function ifs_decode($str){
        $code = 'ifsdt99!*101#';
        
        
        $re = $this->remove_equal(base64_encode(md5($code)));
        $re = preg_replace("/".$re."/", '', trim($str));
               
        $re = base64_decode($re);
        
        $re = preg_replace("/".md5('ifsdt99!')."/", '.', trim($re));
        $re = explode(md5($code), $re);
        return $re;
    }
    
    public function remove_equal($str){
        $re = preg_replace("/===/", '', trim($str));
        $re = preg_replace("/==/", '', trim($re));
        $re = preg_replace("/=/", '', trim($re));
        return $re;
    }
}